import hashlib
from typing import List, Union, Dict

from pydantic import Field

from backend.app.schemas.base import SchemaBase


class DeviceSchemaV2(SchemaBase):
    """设备基础信息"""
    gid: int | str = Field(description="设备ID", default=0)
    sid: int | str = Field(description="商户ID", default=0)
    device_name: str = Field(description="设备名称", default="")
    device_avatar: str = Field(description="图片地址", default="")
    catena: str = Field(description="系列", alias="mod1", default="")
    mod: str = Field(description="型号", default="")
    brand_id: int | str = Field(description="品牌ID", default=0)
    brand_title: str = Field(description="品牌名称", alias="brandTitle", default="")
    class_id: int | str = Field(description="三级分类ID", default=0)
    class_name: str = Field(description="三级分类", default="")
    class_1_id: int | str = Field(description="一级分类ID", default=0)
    class_1_name: str = Field(description="一级分类", default="")
    class_2_id: int | str = Field(description="二级分类ID", default=0)
    class_2_name: str = Field(description="二级分类", default="")
    price: float | str = Field(description="价格", default=0)
    meter: str = Field(description="技术参数", default="")
    meter_clean: str = Field(description="格式化后的设备属性", default="")
    spare_meter: str = Field(description="设备参数", default="")
    address: str = Field(description="店铺地址", default="")
    meter_coding: str = Field(description="物料编码", default="")
    country_title: str = Field(description="国别", default="")
    country: str | float = Field(description="国别标识", default="")
    text: str = Field(description="向量文本描述", default="")
    metadata: str = Field(description="设备属性", default="")
    child_list: List = Field(description="相似设备", default=[])

    def to_vector_desc(self) -> str:
        """转换为向量描述"""
        vector_desc = f"""
        设备ID:{self.gid}, 一级分类:{self.class_1_name}, 二级分类:{self.class_2_name}, 三级分类:{self.class_name}, 品牌：{self.brand_title}, 
        系列:{self.catena}, 型号:{self.mod}, 国别:{self.country_title}, 物料编码：{self.meter_coding}, 设备名称: {self.device_name}, 
        店铺地址：{self.address}, 设备属性：{self.meter_clean or self.meter}
        """

        return vector_desc


class PageInfo(SchemaBase):
    page: int = Field(description="页码", default=1)
    size: int = Field(description="页面大小", default=10)


class DeviceUpdateOrCreateReq(DeviceSchemaV2):
    """设备创建或更新"""
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")


class DeviceBatchOperateReq(DeviceSchemaV2):
    """设备创建或更新"""
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")
    device_list: List[DeviceSchemaV2] = Field(description="设备列表")


class DeviceDetailReq(SchemaBase):
    """设备详情请求"""
    device_name: str = Field(description="标题")
    gid: int = Field(description="产品ID")
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")


class DeviceDeleteReq(SchemaBase):
    """删除设备"""
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")
    gid: int = Field(description="产品ID")


class DeviceBatchDeleteReq(SchemaBase):
    """批量删除设备"""
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")
    gids: List[int] = Field(description="产品ID列表")


class DeviceListReq(PageInfo):
    """设备列表"""
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")
    device_name: str = Field(description="标题", default="")
    gid: int = Field(description="产品ID", default=0)


class DeleteShopReq(SchemaBase):
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")


class TenderDeviceReqV2(DeviceSchemaV2):
    """【标书】搜索设备请求参数"""
    child_system: str = Field(description="子系统", default="")
    number: int | str = Field(description="数量", default="1", validate_default=False)
    unit: str = Field(description="计量单位", default="")
    key: int | str = Field(description="顺序", default=0)
    supply_price_b: Union[int, float, str, None] = Field(description="参考价格", default=0)
    price_list: Dict = Field(description="价格属性", alias="priceList", default={})
    tender_name: str = Field(description="标书设备名称", default="")
    tender_meter: str = Field(description="标书设备参数要求")
    child_list: List = Field(description="相似设备", default=[])

    def hash_id(self):
        """清单问题标识"""
        name = self.tender_name or self.device_name or ""
        meter = self.tender_meter or ""
        encoded_string = (name + meter).encode('utf-8')
        return hashlib.md5(encoded_string).hexdigest()

    def __str__(self) -> str:
        return f"<{self.__class__.__name__}>"
